package com.sanhuo.idms.controller;

import com.sanhuo.file.service.FileService;
import com.sanhuo.idms.bo.RepairSaveBO;
import com.sanhuo.idms.bo.RepairTaskSaveBO;
import com.sanhuo.idms.entity.RepairPersonal;
import com.sanhuo.idms.service.RepairPersonalService;
import com.sanhuo.idms.service.RepairService;
import com.sanhuo.idms.service.RepairTaskService;
import com.sanhuo.idms.utils.SensitiveUtil;
import com.sanhuo.idms.vo.RepairPageVO;
import com.sanhuo.lib.ExceptionUtil;
import com.sanhuo.security.entity.User;
import com.sanhuo.security.service.UserService;
import com.sanhuo.security.uitls.UserHolder;
import com.sanhuo.triild.page.Page;
import com.sanhuo.triild.page.PageSearch;
import com.sanhuo.utils.YunPianUtil;
import com.sanhuo.utils.basic.BeanUtil;
import com.sanhuo.utils.basic.StringUtil;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RequestMapping("repairTask")
@RestController
public class RepairController {

    @Autowired
    private RepairService repairService;
    @Autowired
    private RepairTaskService repairTaskService;
    @Autowired
    private RepairPersonalService repairPersonalService;
    @Autowired
    private UserService userService;


    @RequestMapping("save")
    public void save(RepairSaveBO bo) throws IllegalAccessException {
        SensitiveUtil sensitiveFilter = new SensitiveUtil();
        Set<String> set = sensitiveFilter.getSensitiveWord(bo.getContent(), 1);
        ExceptionUtil.throwException(set.size() > 0, "评论带有敏感词汇,请重新输入,注意文明发言!");
        repairService.save(UserHolder.user(), bo);
    }

    @RequestMapping("list")
    public Page<RepairPageVO> listAll(PageSearch pageSearch) {
        pageSearch.setOrderBy(" r.updateTime desc,r.state asc ");
        return repairService.listAll(UserHolder.user(), pageSearch);
    }

    @RequestMapping("unpass")
    public void changeState(String id) {
        repairService.changeState(UserHolder.user(), id);
    }

    @RequestMapping("deal")
    public void deal(String id) {
        repairService.deal(UserHolder.user(), id);
    }

    @RequestMapping("distri")
    public void saveTask(RepairTaskSaveBO bo) {
        repairTaskService.save(UserHolder.user(), bo);
    }

    @RequestMapping("count")
    public Integer countUnPass() {
        return repairService.countUnPass();
    }

    @RequestMapping("notify")
    public void notify(String id) {
        RepairPersonal repairPersonal = repairPersonalService.findById(id);
        ExceptionUtil.throwException(BeanUtil.isNull(repairPersonal), "不存在此维修人员");
        User user = userService.findById(id);
        ExceptionUtil.throwException(StringUtil.isBlank(user.getPhone()), "该维修人员没有设置手机账号,无法通知");
        String phone = user.getPhone();
        YunPianUtil.sendSms(phone);
    }
}
